package com.rocksdata.mc.database;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Created by 陈堂正 on 2017/8/15.
 */

public class PayrollDBManager {
    private static PayrollDBManager instance = null;
    private SQLiteDatabase db;

    private PayrollDBManager(){
        this.db = DatabaseManager.getDatabase();
    }

    static synchronized PayrollDBManager getInstance(){
        if(instance == null){
            instance = new PayrollDBManager();
        }
        return instance;
    }

    /**
     * 保存工资单数据
     * @param yearMonth 年月
     * @param jsonData 工资信息json包
     */
    public synchronized void savePayrollInfo(String yearMonth, String jsonData){
        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.TPayrollInfo.YEAR_MONTH, yearMonth);
        values.put(DatabaseHelper.TPayrollInfo.JSON_DATA, jsonData);

        db.insert(DatabaseHelper.TPayrollInfo.TABLE_NAME, null, values);
    }

    /**
     * 获取工资单数据
     * @param yearMonth 年月
     * @return
     */
    public synchronized String getPayrollInfo(String yearMonth){
        Cursor c = db.query(DatabaseHelper.TPayrollInfo.TABLE_NAME, null,
                DatabaseHelper.TPayrollInfo.YEAR_MONTH + "=?",
                new String[]{yearMonth},
                null, null, null);
        String result = null;
        if (c.moveToFirst()) {
            result = c.getString(c.getColumnIndex(DatabaseHelper.TPayrollInfo.JSON_DATA));
        }
        c.close();
        return result;
    }
}
